package org.icress.fusion.b2b

import grails.converters.JSON

class SalesOrderAjaxController {
    
    def salesOrderAjaxService

    def contractList = {
        render salesOrderAjaxService.contractList(params.term) as JSON
    }
    
    def industryList = {
        render salesOrderAjaxService.industryList(params.term) as JSON
    }
    
    def projectList221 = {
        render salesOrderAjaxService.projectList221(params.dealer, params.industry, params.term) as JSON
    }
    def projectList224 = {
        render salesOrderAjaxService.projectList224(params.dealer, params.industry, params.term) as JSON
    }
    
    def productList = {
        render salesOrderAjaxService.productList(params.term) as JSON
    }
       
    def productProperty220 = {
        //get product category, recordtype serial number is 2
        def pCategory = salesOrderAjaxService.productCategory (params.serialNumber)
        //get product discount, recordtype serial number is 5
        def pDiscount = salesOrderAjaxService.productDiscount (params.serialNumber)
        //get product delivery cycle, recordtype serial number is 2
        def pDeliveryCycle = salesOrderAjaxService.productDeliveryCycle (params.serialNumber)
        //return json result 
        render(contentType:"text/json"){
            category = pCategory ? pCategory[0] : ""
            discount = pDiscount
            delivery = pDeliveryCycle
            allowZeroPrice = salesOrderAjaxService.isAllowZeroPrice(params.serialNumber)
        }
    }

    def productProperty221 = {
        //get product category, category recordtype serial number is 2
        def pCategory = salesOrderAjaxService.productCategory (params.serialNumber)
        //get product standard discount
        def pDiscount = salesOrderAjaxService.productDiscount (params.serialNumber)
        //get product delivery cycle, category recordtype serial number is 2
        def pDeliveryCycle = salesOrderAjaxService.productDeliveryCycle (params.serialNumber)
        //return json string
        render(contentType:"text/json"){
            category = pCategory ? pCategory[0] : ""
            discount = pDiscount
            delivery = pDeliveryCycle
            allowSpecialDiscount = salesOrderAjaxService.isAllowSpecialDiscount(params.serialNumber)
            allowZeroPrice = salesOrderAjaxService.isAllowZeroPrice(params.serialNumber)
            contractDetails = array{
                def pContractDetail = salesOrderAjaxService.productContractDetail221(params.dealerId, params.project, pCategory ? pCategory[1] : -1L)
                if(pContractDetail != null){
                    contractDetail(
                         'id':pContractDetail[0]
                       , 'discount' : pContractDetail[1]
                       , 'specialDiscount' : pContractDetail[2]
                       , 'finalDiscount':pContractDetail[3]
                       , 'serialNumber' : pContractDetail[4] + '-' + pContractDetail[5]
                   )
                }
            }
            
        }
        
    }
    def productProperty224 = {
        def pCategory = salesOrderAjaxService.productCategory (params.serialNumber)
        def pDiscount = salesOrderAjaxService.productDiscount (params.serialNumber)
        def pDeliveryCycle = salesOrderAjaxService.productDeliveryCycle (params.serialNumber)
        render(contentType:"text/json"){
            category = pCategory ? pCategory[0] : ""
            discount = pDiscount
            delivery = pDeliveryCycle
            price = pPrice
            allowSpecialDiscount = salesOrderAjaxService.isAllowSpecialDiscount(params.serialNumber)
            allowZeroPrice = salesOrderAjaxService.isAllowZeroPrice(params.serialNumber)
            contractDetails = array{
                def pContractDetail = salesOrderAjaxService.productContractDetail224(params.dealerId, params.project, pCategory ? pCategory[1] : -1L)
                if(pContractDetail != null){
                    contractDetail(
                         'id':pContractDetail[0]
                       , 'discount' : pContractDetail[1]
                       , 'specialDiscount' : pContractDetail[2]
                       , 'finalDiscount':pContractDetail[3]
                       , 'serialNumber' : pContractDetail[4] + '-' + pContractDetail[5]
                   )
                }
            }
        }
    }

    def dosage = {
        def dosage = salesOrderAjaxService.dosage(params.masterProduct, params.detailProduct)
        render "${dosage}" 
    }
    def quota = {
        def quota = salesOrderAjaxService.quota(params.masterProduct, params.detailProduct)
        render "${quota}" 
    }

    def deliveryCycle = {
        def deliveryCycle = salesOrderAjaxService.productDeliveryCycle(params.product)
        render view : '/b2b/salesOrder/deliveryCycle', model : [deliveryCycle : deliveryCycle]
    }

    def isAllowSpecialDiscount = {
        def isAllowSpecialDiscount = salesOrderAjaxService.isAllowSpecialDiscount(params.product)
        render "${isAllowSpecialDiscount}"  
    }

    def isAllowZeroPrice = {
        def isAllowZeroPrice = salesOrderAjaxService.isAllowZeroPrice(params.product)
        render "${isAllowZeroPrice}"  
    }
    

    def workflowHistory = {
		def historys = salesOrderAjaxService.workflowHistory(params.salesOrderId)
        render view : '/b2b/salesOrder/workflow_history', model : [workflowHistoryInstanceList : historys, objectId : params.salesOrderId, ownerId : params.ownerId]
    }

    def outBoundMessage = {
		def outBoundMessage = salesOrderAjaxService.outBoundMessage(params.salesOrderId)
        render view : '/b2b/salesOrder/outBound_message', model : [outBoundMessage : outBoundMessage]
    }

    //产品仓管分类
    def productCategoryOfCG = {
        def category = salesOrderAjaxService.productCategory(params.product)
        render "${category[0]?:''}"
    }
    //获取经销商所属办事处
    def getDealerDepartment = {
        def department = salesOrderAjaxService.dealerDeaprtment(params.dealerSerialNumber)
        if(department == null){
            department = []
            department.push('')
            department.push('')
        }
        render "${department[0]?:''}-${department[1]?:''}"
    }

    //获取OA特价申请单单头ID
    def getContractId = {
        def contractId = salesOrderAjaxService.getContractId(params.contractDetailId?:0)
        render "${contractId?:''}"
    }

    //获取OA特价申请单编号
    def getContractDetailSerialNumber = {
        def contractDetailSerialNumber = salesOrderAjaxService.getContractDetailSerialNumber(params.contractDetailId?:0)
        if(contractDetailSerialNumber)
            render "${contractDetailSerialNumber[0]?:''}-${contractDetailSerialNumber[1]?:''}"
        else
            render ""
    }

}
